from DrissionPage import Chromium,ChromiumOptions
from pprint import pprint
import threading
import concurrent.futures

# co = ChromiumOptions.use_system_user_path()
#
# browser = Chromium(co)
browser = Chromium()

tab = browser.new_tab('https://www.wenxuewang.net/39/39076/')

chapters_xpath = 'x:/html/body/section[2]/div/div/div/div[1]/ul'
chapters_dict = {}

# 从网页中提取章节标题和链接
for chapter in tab(chapters_xpath).eles('t:a'):
    chapters_dict[chapter.text] = chapter.link

print(chapters_dict)
list_chapter = list(chapters_dict.keys())

def fetch_and_save_chapter(chapter_name, chapter_link):
    new_tab = browser.new_tab(chapter_link)
    # 1/3
    chapter_content = new_tab('x:/html/body/section/div/div/article').text
    # 2/3
    new_tab.ele('x://*[@id="next"]').click()
    chapter_content1 = new_tab('x:/html/body/section/div/div/article').text
    # 3/3
    new_tab.ele('x://*[@id="next"]').click()
    chapter_content3 = new_tab('x:/html/body/section/div/div/article').text
    # 合并章节内容
    chapter_content = chapter_content + chapter_content1 + chapter_content3
    print(f'正在下载{chapter_name}...')
    tab.wait(1)
    # 将章节内容写入文件
    with open(chapter_name + '.txt', 'w',encoding='utf-8') as f:
        f.write(chapter_content)
        # 关闭新标签页
        # new_tab.close()
        threading.Thread(target=new_tab.close).start()
# # 单线程
# for aa in list_chapter:
#     fetch_and_save_chapter(aa, chapters_dict[aa])

# 多线程
def main():
    # 创建一个线程池，最多允许4个线程同时运行
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        # 提交任务
        future_to_url = {executor.submit(fetch_and_save_chapter, chapter_name, chapter_link): chapter_name
                         for chapter_name, chapter_link in chapters_dict.items()}
        # 等待所有任务完成

if __name__ == '__main__':
    main()

